import json
import unittest
import requests
import app
from api.login import LoginAPI
from parameterized import parameterized


# 参数化
def build_data():
    file = app.BASE_DIR + "/data/login.json"
    data_login = []
    with open(file, encoding='utf-8') as f:
        json_data = json.load(f)
        for case_data in json_data:
            mobile = case_data.get('mobile')
            password = case_data.get('password')
            success = case_data.get('success')
            code = case_data.get("code")
            message = case_data.get('message')
            data_login.append((mobile, password, success, code, message))
        print(data_login)
        return data_login


class TestLogin(unittest.TestCase):
    def setUp(self):
        # 实例化接口类
        self.login_api = LoginAPI()
        self.session = requests.Session()

    def tearDown(self):
        if self.session:
            self.session.close()

    @parameterized.expand(build_data)
    def test_login(self, mobile, password, success, code, message):
        response = self.login_api.login(self.session, mobile, password)
        print(response.json())
        self.assertEqual(success, response.json().get('success'))
        self.assertEqual(code, response.json().get('code'))
        self.assertIn(message, response.json().get('message'))

